#include "VSRasterizerState.h"
#include "VSShader.h"
#include "VSMaterial.h"
#include "VSGraphicInclude.h"
#include "VSResourceManager.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSRasterizerState,VSBind)
VSPointer<VSRasterizerState> VSRasterizerState::Default;
BEGIN_ADD_PROPERTY(VSRasterizerState,VSBind)
REGISTER_PROPERTY(m_RasterizerDesc,RasterizerDesc,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSRasterizerState)
ADD_INITIAL_FUNCTION_WITH_PRIORITY(InitialDefaultState)
ADD_TERMINAL_FUNCTION(TerminalDefaultState)
IMPLEMENT_INITIAL_END
bool VSRasterizerState::InitialDefaultState()
{
	VSRasterizerDesc RasterizerDesc;
	Default = VSResourceManager::CreateRasterizerState(RasterizerDesc);
	if(!Default)
		return 0;
	return 1;
}
bool VSRasterizerState::TerminalDefaultState()
{
	Default = NULL;
	return 1;
}
bool VSRasterizerState::OnLoadResource(VSResourceIdentifier *&pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnLoadRasterizerState(this,pID))
		return 0;
	return 1;
}
bool VSRasterizerState::OnReleaseResource(VSResourceIdentifier *pID)
{
	if(!m_pUser)
		return 0;
	if(!m_pUser->OnReleaseRasterizerState(pID))
		return 0;
	return 1;
}
VSRasterizerState::VSRasterizerState()
{
	m_bIsStatic = 1;
}
VSRasterizerState::~VSRasterizerState()
{
	ReleaseResource();
}
unsigned int VSRasterizerState::DiskUse()const
{
	unsigned int Use = VSBind::DiskUse();

	Use += m_RasterizerDesc.DiskUse();

	return Use;
}
bool VSRasterizerState::Save(VSStream & rStream,unsigned int &iSaveUse)const
{
	if(!VSObject::Save(rStream,iSaveUse))
		return false;


	if (!m_RasterizerDesc.Save(rStream,iSaveUse))
	{
		return 0;
	}
	return 1;
}
bool VSRasterizerState::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!VSObject::Load(rStream,iSaveUse))
		return false;


	if(!m_RasterizerDesc.Load(rStream,iSaveUse))
		return 0;

	return 1;
}
bool VSRasterizerState::Clone(const VSObject *pObject,unsigned int uiType)
{
	const VSRasterizerState* Temp = DynamicCast<VSRasterizerState>(pObject); 
	if(!Temp)
		return 0;

	if(VSBind::Clone(pObject,uiType))
	{

		m_RasterizerDesc = Temp->m_RasterizerDesc;


		return 1;
	}
	else
	{
		return 0;

	}
}
bool VSRasterizerState::Link(VSStream & rStream)
{
	if(!VSObject::Link(rStream))
		return 0;

	return 1;
}
bool VSRasterizerState::Register(VSStream & rStream)const
{
	if (rStream.IsObjectRegister(this))
	{
		return 1;
	}
	if(!VSBind::Register(rStream))
		return 0;


	return 1;
}
IMPLEMENT_RTTI(VSRasterizerDesc,VSObject)
BEGIN_ADD_PROPERTY(VSRasterizerDesc,VSObject)
REGISTER_PROPERTY(m_bWireEnable,WireEnable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_uiCullType,CullType,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_bClipPlaneEnable,ClipPlaneEnable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_bScissorTestEnable,ScissorTestEnable,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_fDepthBias,DepthBias,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSRasterizerDesc)
IMPLEMENT_INITIAL_END
bool VSRasterizerDesc::Save(VSStream & rStream,unsigned int &iSaveUse)const
{

	if(!rStream.Write(&m_bWireEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);

	if(!rStream.Write(&m_uiCullType,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Write(&m_bClipPlaneEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);

	if(!rStream.Write(&m_bScissorTestEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);

	if(!rStream.Write(&m_fDepthBias,sizeof(VSREAL)))
		return false;
	iSaveUse += sizeof(VSREAL);
	return true;

}
bool VSRasterizerDesc::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if(!rStream.Read(&m_bWireEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);

	if(!rStream.Read(&m_uiCullType,sizeof(unsigned char)))
		return false;
	iSaveUse += sizeof(unsigned char);

	if(!rStream.Read(&m_bClipPlaneEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);

	if(!rStream.Read(&m_bScissorTestEnable,sizeof(bool)))
		return false;
	iSaveUse += sizeof(bool);	

	if(!rStream.Read(&m_fDepthBias,sizeof(VSREAL)))
		return false;
	iSaveUse += sizeof(VSREAL);
	return true;
}